<template>
  <div v-loading="loading">
    <a @click="handleOpenDetail">查看</a>
    <SecretSetting @register="registerDetail" />
    <VerifyModal @register="registerVerify" :submitCallback="handleSubmitVerify" />
  </div>
</template>
<script lang="ts" setup>
  import { ref } from "vue"
  import { useModal } from "/@/components/Modal"
  import SecretSetting from "./SecretSetting.vue"
  import { BizType } from "/@/api/sys/model/msgPushModel"
  import { VerifyModal } from "/@/components/BizVerify"
  import { getSecret } from "../api"
  const emit = defineEmits(["register"])
  const props = defineProps({
    record: { type: Object },
  })
  const loading = ref(false)

  const [registerDetail, { openModal: openDetail }] = useModal()
  const [registerVerify, { openModal: openVerify }] = useModal()
  const handleOpenDetail = async () => {
    const { id, isSettingSecret, isShowSecretVerify, tenantOwner } = props.record as any
    if (isSettingSecret && isShowSecretVerify) {
      openVerify(true, {
        verifyType: "verifyEmail",
        email: {
          value: tenantOwner?.email,
          encryptData: tenantOwner?.emailEncryptData,
        },
        phone: {
          value: tenantOwner?.phone,
          encryptData: tenantOwner?.phoneEncryptData,
        },
        title: "请联系企业拥有者进行安全认证",
        bizType: BizType.SHOW_OAUTH_CLIENT_SECRET,
      })
      return
    } else {
      const clientDetail = await getSecret({ id: id })
      openDetail(true, { isDetail: true, record: clientDetail })
    }
  }

  const handleSubmitVerify = async (data) => {
    console.log("submitVerify: ", data)
    loading.value = true
    try {
      const verifyData = {
        taskCode: data.taskCode,
        bizType: data.bizType,
        verifyCode: data.verifyCode,
        encryptData: data.encryptData,
      }
      const clientDetail = await getSecret({ id: props.record?.id, ...verifyData })
      if (props.record) {
        props.record.isShowSecretVerify = false
      }
      openDetail(true, { record: clientDetail, isDetail: true })
    } finally {
      loading.value = false
    }
  }
</script>

<style lang="less"></style>
